<!DOCTYPE html>
<html class="pixel-ratio-2 retina android android-6 android-6-0">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta name="format-detection" content="telephone=no" />
    <title>首页</title>
    <link rel="stylesheet" href="assets/css/style.css">
	<link rel="stylesheet" href="assets/css/mui.min.css">
	<link rel="stylesheet" href="assets/font/iconfont.css">
	<script src="assets/js/vue.min.js"></script>
	<script src="assets/js/mui.min.js"></script>
	<script src="assets/js/common.js"></script>
	<script src="UpGw/utils.js"></script>
	<script src="assets/js/Bluetooth.js"></script>
	<script src="assets/js/dataBase.js"></script>
</head>
	<body>
		<div id="H5_MUI">
			<!-- 标题栏 -->
			<div class="navigation-bar">
				<div class="navigation-bar-title">终端编辑</div>
				<div class="navigation-left">
					<div class="navigation-left-title" v-on:click="CommonUtil.clicked('collect.html','终端管理')"><li class="iconfont fs15">&#xe644;</li></div>
				</div>
			<!-- 	<div class="navigation-right">
					<div class="navigation-right-title" >
						<i class="iconfont fs16">&#xe784;</i>
					</div>
				</div> -->
			</div>
			<!-- 内容 -->
			<div class="content">
				<form class="mui-input-group fs14">
					<div class="mui-input-row">
						<label style="width: 30%;">终端地址</label>
						<input type="text" class="mui-input-clear content-input"  placeholder="000000000000" v-model="CollectAddr">
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">终端名称</label>
						<input type="text"  class="mui-input-clear content-input" v-model="CollectName" placeholder="终端名称">
					</div>
					<div class="mui-button-row">
						<button type="button" class="mui-btn"  style="background-color: #00796B;color: #FFFFFF;"  v-on:click="insertData()" >保存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<button type="button" class="mui-btn mui-btn-danger" v-on:click="deleteData()">删除</button>
					</div>
				</form>
			</div>
		</div>
	</body>
</html>
<script>
	// 初始化
	var vm = new Vue({
		el: '#H5_MUI',
		data: {
			collectAddr:'000000000000',
			CollectAddr:'',
			CollectName:'',
		},
		created: function () {
			this.plusReady();
		},
		methods: {
			plusReady:function(){
				var that = this;
				document.addEventListener('plusready', function(){
					that.collectAddr = CommonUtil.getStorage('selCollectAddr');
					let sqlStr = "select * from Collect";
					let collectAddr = window.location.search.substr(1);
					if(collectAddr !='') 
					{
						sqlStr =  "select * from Collect where CollectAddr = '"+collectAddr+"'";
						DB.selectSQL(sqlStr,function(data){
							console.log(JSON.stringify(data));
							that.CollectAddr = data[0].CollectAddr;
							that.CollectName = data[0].CollectName;
						});
					}
				},false);
			},
			insertData:function(){
				//终端地址12位？
				if(this.CollectAddr == ''){
					mui.toast('终端地址不能为空');
					return false;
				}
				let collectAddr = Util.padLeft(this.CollectAddr,9,'0');//补零
				//判断是否存在
				let that = this;
				let sqlStr = "select * from Collect where CollectAddr = '"+collectAddr+"'";
				DB.selectSQL(sqlStr,function(data){
					console.log(data.length);
					if(data.length >= 1){
						let updateStr = "update Collect set CollectName = '"+that.CollectName+"' where CollectAddr = '"+collectAddr+"'";
						DB.updateSQL(updateStr,function(data){
							if(data == 'success'){
								mui.toast('数据成功编辑');
								window.location.href = 'collect.html';
							}else if(data == 'fail'){
								mui.toast('数据编辑失败');
							}
						});
					}
					if(data.length <= 0){
						DB.insertToSQL('INSERT INTO Collect VALUES ("'+that.CollectName+'", "'+collectAddr+'")',function(data){
							if(data == 'success'){
								mui.toast('数据成功保存');
								window.location.href = 'collect.html';
							}else if(data == 'fail'){
								mui.toast('数据保存失败');
							}
						});
					}
				});
			},
			deleteData:function(){
				let that = this;
				let sqlStr = "select * from Collect where CollectAddr = '"+this.CollectAddr+"'";
				DB.selectSQL(sqlStr,function(data){
					if(data.length <= 0){
						mui.toast('数据不存在，请核实终端地址');
						return false;
					}
					if(data.length >= 1){
						let btnArray = ['否', '是'];
						mui.confirm('是否删除终端'+that.CollectAddr, '删除提示', btnArray, function(e) {
							if (e.index == 1) {
								DB.deleteSQL('delete from Collect where CollectAddr = "'+that.CollectAddr+'"',function(data){
									if(data == 'success'){
										mui.toast('数据成功删除');
										window.location.href = 'collect.html';
									}else if(data == 'fail'){
										mui.toast('数据删除失败');
									}
								});
							} else {
								mui.toast('成功取消');
							}
						})
					}
				});
			}
		}
	})
	 
</script>
